CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake )

SET_TARGET_PROPERTIES ( ${PROJECT_EXECUTABLES} PROPERTIES
	LINKER_LANGUAGE CXX
)

INCLUDE_DIRECTORIES (
	${CMAKE_CURRENT_BINARY_DIR}/include/rbdl
	${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/urdfparser/include
)

SET ( URDFREADER_SOURCES
	urdfreader.cc
	)

IF (DEFINED ENV{ROS_ROOT})
	MESSAGE (STATUS "ROS found: $ENV{ROS_ROOT}")
	find_package(catkin REQUIRED COMPONENTS urdf)
	OPTION (RBDL_USE_ROS_URDF_LIBRARY "Use the URDF library provided by ROS" ON)
ELSE ()
	SET (RBDL_USE_ROS_URDF_LIBRARY FALSE)
ENDIF ()

IF (RBDL_USE_ROS_URDF_LIBRARY)
	SET (URDFREADER_DEPENDENCIES
		${urdf_LIBRARIES}
		)
ELSE()
  ADD_SUBDIRECTORY(thirdparty)			
  SET(CMAKE_CXX_STANDARD 17)
	SET (URDFREADER_SOURCES	
		${URDFREADER_SOURCES}
		)
	SET (URDFREADER_DEPENDENCIES
    urdfparser
		)
ENDIF()

IF (MSVC)
  SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /permissive- /std:c++17")
ENDIF (MSVC)

IF(RBDL_BUILD_EXECUTABLES)
ADD_EXECUTABLE (rbdl_urdfreader_util rbdl_urdfreader_util.cc)
ENDIF(RBDL_BUILD_EXECUTABLES)

IF (RBDL_BUILD_STATIC)
	ADD_LIBRARY ( rbdl_urdfreader-static STATIC ${URDFREADER_SOURCES} )

	IF (NOT WIN32)
		SET_TARGET_PROPERTIES ( rbdl_urdfreader-static PROPERTIES PREFIX "lib")
	ENDIF (NOT WIN32)
	SET_TARGET_PROPERTIES ( rbdl_urdfreader-static PROPERTIES OUTPUT_NAME "rbdl_urdfreader")

	TARGET_LINK_LIBRARIES (rbdl_urdfreader-static
		rbdl-static
		${URDFREADER_DEPENDENCIES}
		)

  IF(RBDL_BUILD_EXECUTABLES)
	TARGET_LINK_LIBRARIES (rbdl_urdfreader_util
		rbdl_urdfreader-static
		rbdl-static
		)

	INSTALL (TARGETS rbdl_urdfreader_util
		  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  		ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	)
  ENDIF(RBDL_BUILD_EXECUTABLES)

	INSTALL (TARGETS rbdl_urdfreader-static
		  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  		ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	)
ELSE (RBDL_BUILD_STATIC)
	ADD_LIBRARY ( rbdl_urdfreader SHARED ${URDFREADER_SOURCES} )
	SET_TARGET_PROPERTIES ( rbdl_urdfreader PROPERTIES
		VERSION ${RBDL_VERSION}
		SOVERSION ${RBDL_SO_VERSION}
  )

	TARGET_LINK_LIBRARIES (rbdl_urdfreader
    rbdl
		${URDFREADER_DEPENDENCIES}
		)
  IF(RBDL_BUILD_EXECUTABLES)
	TARGET_LINK_LIBRARIES (rbdl_urdfreader_util
		rbdl_urdfreader
		)
	INSTALL (TARGETS rbdl_urdfreader_util
		RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
		LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
		ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
		)
  ENDIF(RBDL_BUILD_EXECUTABLES)

	INSTALL (TARGETS rbdl_urdfreader
		RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
		LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
		ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
		)
ENDIF (RBDL_BUILD_STATIC)

IF (RUN_AUTOMATIC_TESTS)
  IF (URDF_BUILD_TEST)
  ADD_CUSTOM_COMMAND (TARGET rbdl_urdfreader
    POST_BUILD
    COMMAND ./thirdparty/urdfparser/test_library
    COMMENT "Running automated tests..."
    )
  ENDIF(URDF_BUILD_TEST)
ENDIF(RUN_AUTOMATIC_TESTS)

FILE ( GLOB headers
	"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
	)

INSTALL ( FILES ${headers} 
	DESTINATION
	${CMAKE_INSTALL_INCLUDEDIR}/rbdl/addons/urdfreader
	)
